<template>
  <div>
    <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel"
               :title="'输入条件'" append-to-body width="970px">
      <el-form ref="form" :inline="true" :model="form" size="small" label-width="96px">
        <el-form-item label="生成条数" prop="total">
          <el-input v-model="form.total"/>
        </el-form-item>
        <el-form-item label="表名" prop="tableName">
          <el-input v-model="form.tableName.toString()"/>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" ref="table" :data="tableData" :model="tableData" size="small" style="width: 100%;">
        <el-table-column type="selection" width="55"/>
        <el-table-column label="列名" prop="columnName"/>
        <el-table-column label="注释" prop="columnComment"/>
        <el-table-column label="数据类型" align="center" prop="dataType"/>
        <el-table-column label="生成规则">
          <template slot-scope="scope">
            <el-select
              v-model="selectGeneratorDataRuleId[scope.$index]"
              collapse-tags
              style="margin-left: 20px;"
              placeholder="请选择"
            >
              <el-option
                v-for="(item,index) in scope.row.generatorDataRule"
                :key="index"
                :value="item.dataRuleId"
                :label="item.dataRuleName"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {exportDataToExcelZip} from '@/api/code-tools'
    import {downloadFileFunction} from '@/api/downloadFile'
    import checkPermission from '@/utils/permission'
    import {parseTime} from '@/utils/index'
    import initData from '@/mixins/initData'
    import {getColumnInfoFromTableName, generatorDataIntoTable} from '@/api/code-tools'

    export default {
        mixins: [initData],
        data() {
            return {
                dialog: false,
                loading: false,
                form: {tableName: '', condition: '', total: 100},
                tableData: [],
                selectGeneratorDataRuleId: []
            }
        },
        methods: {
            parseTime,
            checkPermission,
            cancel() {
                this.resetForm()
            },
            getListData(tableName) {
                getColumnInfoFromTableName({
                    tableName: tableName
                }).then(res => {
                    this.loading = false;
                    this.tableData = res.data;
                    const selectGeneratorDataRuleMap = {};
                    this.selectGeneratorDataRuleId = [];
                    res.data.forEach(item => {
                        selectGeneratorDataRuleMap[item.columnName] = item.generatorDataRule[0].dataRuleId;
                        this.selectGeneratorDataRuleId.push(item.generatorDataRule[0].dataRuleId);
                    });
                }).catch(() => {
                    this.loading = false;
                })
            },
            doSubmit() {
                let idsTableNames = this.form.tableName;
                let total = this.form.total;
                let submitTableData = this.$refs.table.tableData;
                console.log(submitTableData);
                let selectGeneratorDataRuleId = this.selectGeneratorDataRuleId;
                let arr2 = [];
                submitTableData.forEach((item, index) => {
                    let tmp = selectGeneratorDataRuleId[index];
                    arr2.push({
                        columnName: item.columnName,
                        dataType: item.dataType,
                        selectGeneratorDataRuleId: tmp
                    })
                });
                console.log(arr2);
                console.log(JSON.stringify(arr2));
                console.log(JSON.stringify({
                    tableName: idsTableNames,
                    mysqlColumnInfo: arr2,
                    total: total
                }));
                generatorDataIntoTable({
                        tableName: idsTableNames,
                        mysqlColumnInfo: arr2,
                        total: total
                    }
                ).then(res => {
                    this.loading = false;
                    console.log(res.data)
                }).catch(err => {
                    this.loading = false;
                    console.log(err);
                });

                this.dialog = false;
            },
            resetForm() {
                this.dialog = false;
                this.$refs['form'].resetFields();
            },
            commonGeneratorCodeZip(generatorCodeParams, outputFileName) {
                console.log('aaa' + generatorCodeParams);
                exportDataToExcelZip(generatorCodeParams).then(res => {
                    this.loading = false;
                    downloadFileFunction(res, outputFileName);
                    this.$notify({
                        title: '下载成功',
                        type: 'success',
                        duration: 2500
                    })
                }).catch(err => {
                    this.loading = false;
                    console.log(err)
                })
            }

        }
    }
</script>
